package com.alex.server.controller;


import com.alex.server.pojo.Admin;
import com.alex.server.pojo.RespBean;
import com.alex.server.pojo.Role;
import com.alex.server.service.IAdminRoleService;
import com.alex.server.service.IAdminService;
import com.alex.server.service.IRoleService;
import com.alex.server.service.impl.AdminRoleServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author alex
 * @since 2022-08-30
 */
@RestController
@RequestMapping("/system/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IAdminRoleService adminRoleService;

    /**
     * 根据关键词来获取操作员
     * @param keywords
     * @return
     */
    @ApiOperation(value = "获取所有操作员")
    @GetMapping
    public List<Admin> getAllAdmins(@RequestParam String keywords){
        return adminService.getAllAdmins(keywords);
    }
    @ApiOperation(value = "更新操作员")
    @PutMapping("/")
    public RespBean updateAdmin(@RequestBody Admin admin){
        if (adminService.updateById(admin)){
            return RespBean.success("更新成功");
        }else {
            return RespBean.error("更新失败");
        }
    }
    @ApiOperation(value = "删除操作员")
    @DeleteMapping("/{id}")
    public RespBean deleteAdmin(@PathVariable Integer id){
        if (adminService.removeById(id)){
            return RespBean.success("删除成功");
        }else {
            return RespBean.error("删除失败");
        }
    }
    @ApiOperation(value = "查询所有角色")
    @GetMapping("/roles")
    public List<Role> getAllRoles(){
        List<Role> list = roleService.list();
        return list;
    }
    @ApiOperation("跟新操作员角色")
    @PutMapping("/roles")
    public RespBean updateRole(Integer adminId ,Integer[] rid){
       return adminRoleService.updateRole(adminId,rid);
    }
}
